package menupopupplugin2.popup.actions;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class InputConnectionURLPage extends CustomWizardPage {
	private Text tfConnectionURL;
	private Text tfUsername;
	private Text tfPassword;
	private Composite container;
	private Combo combo;

	private String connectionURL;
	private String username;
	private String password;

	public InputConnectionURLPage() {
		super("InputConnectionURLPage");
		setTitle("Input connection URL page");
		setDescription("Page used for let user input connection URL and some "
				+ "necessary information.");
	}

	@Override
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;

		Label lblTypeOfDatabaseSystem = new Label(container, SWT.NULL);
		lblTypeOfDatabaseSystem.setText("Type of database system");

		combo = new Combo(container, SWT.READ_ONLY);
		combo.setItems(new String[] { "MySQL", "MS SQLServer" });
		combo.addSelectionListener(new SelectionAdapterImpl());

		// ----------------------------------------------------------

		Label lblConnectionURL = new Label(container, SWT.NULL);
		lblConnectionURL.setText("Fill connection URL");

		tfConnectionURL = new Text(container, SWT.BORDER | SWT.SINGLE);
		tfConnectionURL.setText("");
		tfConnectionURL.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				String txtConectionURL = tfConnectionURL.getText();
				if (!txtConectionURL.isEmpty())
					setPageComplete(true);
				else
					setPageComplete(false);
			}
		});

		Label lblUsername = new Label(container, SWT.NULL);
		lblUsername.setText("Fill user name");

		tfUsername = new Text(container, SWT.BORDER | SWT.SINGLE);
		tfUsername.setText("");

		Label lblPassword = new Label(container, SWT.NULL);
		lblPassword.setText("Fill password");

		tfPassword = new Text(container, SWT.BORDER | SWT.SINGLE);
		tfPassword.setText("");

		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		tfConnectionURL.setLayoutData(gd);
		tfUsername.setLayoutData(gd);
		tfPassword.setLayoutData(gd);

		// Required to avoid an error in the system
		setControl(container);
		setPageComplete(false);

	}

	public void transitionFromTextControl() {
		connectionURL = tfConnectionURL.getText();
		username = tfUsername.getText();
		password = tfPassword.getText();
	}

	public String getConnectionURL() {
		return connectionURL;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	@Override
	public boolean isFinish() {
		return !"".equals(tfConnectionURL.getText());
	}

	private class SelectionAdapterImpl extends SelectionAdapter {
		public void widgetSelected(SelectionEvent e) {
			if (combo.getText().equalsIgnoreCase("MySQL")) {
				tfConnectionURL.setText("jdbc:mysql://");
				tfConnectionURL
						.setToolTipText("jdbc:mysql://[server-name]:[server-post]/[database-name]");
				tfUsername.setText("root");
			} else if (combo.getText().equalsIgnoreCase("MS SQLServer")) {
				tfConnectionURL.setText("jdbc:sqlserver://");
				tfConnectionURL
						.setToolTipText("jdbc:sqlserver:/[server-name]:[server-post]/[database-name]");
				tfUsername.setText("sa");
			}
		}
	}
}
